﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Web;
using System.Web.UI;
using System.Drawing;


namespace HDI.Charts2
{

    public class Chart : Control
    {

        public List<Series> Daten = new List<Series>();
        protected String errors = ""; 

        protected Legend _Legend;

        public Legend Legend
        {
            get { return _Legend; }
        }

        protected ValueAxis _vAxis;

        public ValueAxis ValueAxis
        {
            get { return _vAxis; }
        }

        protected CategoryAxis _cAxis;

        public CategoryAxis CategoryAxis
        {
            get { return _cAxis; }
        }

        protected ChartCursor _cCursor;

        public ChartCursor ChartCursor
        {
            get { return _cCursor; }
        }

        protected string html = "";
        public String Title { get; set; }
        public String JS { get; set; }
        public String CSS { get; set; }
        public String Height = "640px";

        protected Dictionary<String, String> _Options = new Dictionary<string, string>();
        public void setOption(String key, String value)
        {
            if (!_Options.Keys.Contains(key))
            {
                _Options.Add(key, value);
            }
            else
            {
                _Options[key] = value;
            }
        }

        public Dictionary<string, string> Option
        {
             get {return _Options;} 
           
        }

        public Chart(string title)
        {
            init();
            this.Title = title;
            

        }

        public Chart(string title, string[] series):this (title)
        {
            foreach (string serie in series)
            {
                this.addSeries(serie);
            }

        }


        public virtual void init()
        {
            JS = "";
            CSS = "";
        }

        public virtual Data addData(int series, string name, string value)
        {
            Data tmp = this.Daten[series].addData(name, value);
            
            return tmp;
        }

        public virtual Series addSeries(string Name)
        {
            Series serie = new Series(Name);
            serie.Chart = this; 
            this.Daten.Add(serie);
            return serie;
        }


        public virtual string getHTML()
        {
            return html;
        }

        public Series this[string name]
        {
            get
            {
                try
                {
                    return Daten.Single(x => x.Name.Equals(name));
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ScriptManager.RegisterStartupScript(this, this.GetType(), ClientID+"_HDICharts", JS, true);
            Page.Header.Controls.Add(new LiteralControl(String.Format("<link  href='{0}' rel='stylesheet' type='text/css' />", Page.ClientScript.GetWebResourceUrl(this.GetType(), "HDI.Charts2.css.style.css"))));
      
        }
        
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                base.Render(writer);
                if (errors != "")
                    writer.Write(errors);
               
                writer.Write(getHTML());

            }catch(Exception ex)
            {
                writer.Write(ex.ToString());
            }
            
        }
    }
}
